package cn.xiaosiyuan.comprehensive;

import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class Test2020年11月2日 {
    public static void main(String[] args) {
//        System.out.println(formatString(123131231));
//        System.out.println(formatString2(123131231));


/*        String[] strings = {""," ","0.0","0","000","0.00","000000.00","00000.00"};
        for (int i = 0; i < strings.length; i++) {
//            System.out.println(amountFormat(strings[i]));
            System.out.println(rateFormat(strings[i]));
        }*/

        double a = -1;
        Map<String,Object> map = new HashMap<>();
        map.put("test", a);
        System.out.println(map.get("test").toString());

    }

    private static String rateFormat(String string) {
        return amountFormat(string) + "%";
    }

    private static String amountFormat(String s) {
        // 考虑为空的情况
        if(StringUtils.isBlank(s))
            return "0.00";
        StringBuilder stringBuilder = new StringBuilder();
        if(!s.contains("."))
            s += ".00";
        stringBuilder.append(s.split("\\.")[0]);
        // 从后往前每隔三位插入逗号
        int last = stringBuilder.length();
        for(int i = last - 3; i > 0; i-=3) {
            stringBuilder.insert(i,',');
        }
        stringBuilder.append(".");
        stringBuilder.append(s.split("\\.")[1]);
        // 考虑为1位的情况
        if(1 == s.split("\\.")[1].length())
            stringBuilder.append("0");
        return stringBuilder.toString();
    }




    //格式化数字表示
    public static String formatString(float data) {
        DecimalFormat df = new DecimalFormat("#,###.00");
        return df.format(data);
    }
    public static String formatString2(float data) {
        DecimalFormat df = new DecimalFormat("#,###");
        return df.format(data);
    }


    // 考虑保留位数，考虑不同数据类型
}
